package com.hbx.residential_service.dao;

import com.hbx.residential_service.domain.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserDao {
    @Select("select * from owners")
    public List<User> findAll(int page, int size) throws Exception;
    @Select("select * from owners where userid=#{userid}")
    public User findById(String userid)throws Exception;
    @Select("select * from owners where username=#{username} and userpwd=#{userpwd}")
    User userLogin(@Param("username") String username, @Param("userpwd")String userpwd);
    @Select("select * from complaints where cpUserId=(select userid from owners where username=#{username}) order by cpId DESC")
    @Results({
            @Result(id=true,property = "cpId",column = "cpId"),
            @Result(property = "cpTitle",column = "cpTitle"),
            @Result(property = "cpContent",column = "cpContent"),
            @Result(property = "cpDate",column = "cpDate"),
            @Result(property = "cpStatus",column = "cpStatus"),
            @Result(property = "cpHandleDate",column = "cpHandleDate"),

            @Result(property = "user",column = "cpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "cpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    List<Complaint> userFindCp(@Param("username") String username,@Param("page") int page,@Param("size") int size);

    @Select("select * from repairs where rpUserId=(select userid from owners where username=#{username}) order by rpId DESC")
    @Results({
            @Result(id=true,property = "rpId",column = "rpId"),
            @Result(property = "rpTitle",column = "rpTitle"),
            @Result(property = "rpContent",column = "rpContent"),
            @Result(property = "rpDate",column = "rpDate"),
            @Result(property = "rpStatus",column = "rpStatus"),
            @Result(property = "rpHandleDate",column = "rpHandleDate"),

            @Result(property = "user",column = "rpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "rpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    List<Repair> userFindRp(@Param("username") String username,@Param("page1") int page1,@Param("size1") int size1);

    @Select("select * from notices order by ntId DESC limit 2")
    List<Notice> FindNewNt();

    @Select("select * from houses where houseUserId=(select userid from owners where username=#{username})")
    @Results({
            @Result(id=true,property = "houseId",column = "houseId"),
            @Result(property = "houseNum",column = "houseNum"),
            @Result(property = "houseBuild",column = "houseBuild"),
            @Result(property = "houseStatus",column = "houseStatus"),
            @Result(property = "houseDate",column = "houseDate"),
            @Result(property = "user",column = "houseUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById"))
    })
    List<House> userFindHs(String username);

    @Select("select * from carsites where csUserId=(select userid from owners where username=#{username})")
    @Results({
            @Result(id=true,property = "csId",column = "csId"),
            @Result(property = "csNum",column = "csNum"),
            @Result(property = "csStatus",column = "csStatus"),
            @Result(property = "csCarNum",column = "csCarNum"),
            @Result(property = "user",column = "csUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById"))
    })
    List<CarSite> userFindCs(String username);

    @Select("select * from pays where payUserId=(select userid from owners where username=#{username}) and payStatus=0")
    @Results({
            @Result(id=true,property = "payId",column = "payId"),
            @Result(property = "payType",column = "payType"),
            @Result(property = "payMoney",column = "payMoney"),
            @Result(property = "payStatus",column = "payStatus"),
//            @Result(property = "payDate",column = "payDate"),
            @Result(property = "user",column = "payUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById"))
    })
    List<Pay> userFindPy(String username);

    @Select("select usersex from owners where username=#{username}")
    String getUserSex(String username);

    @Insert("insert into complaints(cpTitle,cpContent,cpDate,cpStatus,cpHandleDate,cpUserId,cpAdminId) values(#{cpTitle},#{cpContent},#{cpDate},#{cpStatus},#{cpHandleDate},#{user.userid},#{admin.adminid})")
    @Results({
            @Result(id=true,property = "cpId",column = "cpId"),
            @Result(property = "cpTitle",column = "cpTitle"),
            @Result(property = "cpContent",column = "cpContent"),
            @Result(property = "cpDate",column = "cpDate"),
            @Result(property = "cpStatus",column = "cpStatus"),
            @Result(property = "cpHandleDate",column = "cpHandleDate"),

            @Result(property = "user",column = "cpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "cpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    void addCp(Complaint complaint);

    @Select("select * from owners where username=#{username}")
    User findByUserName(String username);

    @Insert("insert into repairs(rpTitle,rpContent,rpDate,rpStatus,rpHandleDate,rpUserId,rpAdminId) values(#{rpTitle},#{rpContent},#{rpDate},#{rpStatus},#{rpHandleDate},#{user.userid},#{admin.adminid})")
    @Results({
            @Result(id=true,property = "rpId",column = "rpId"),
            @Result(property = "rpTitle",column = "rpTitle"),
            @Result(property = "rpContent",column = "rpContent"),
            @Result(property = "rpDate",column = "rpDate"),
            @Result(property = "rpStatus",column = "rpStatus"),
            @Result(property = "rpHandleDate",column = "rpHandleDate"),

            @Result(property = "user",column = "rpUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById")),
            @Result(property = "admin",column = "rpAdminId",javaType = Admin.class,one = @One(select = "com.hbx.residential_service.dao.AdminDao.findById"))
    })
    void addRp(Repair repair);

    @Select("select * from pays where payUserId=(select userid from owners where username = #{username}) and payStatus =0")
    @Results({
            @Result(id=true,property = "payId",column = "payId"),
            @Result(property = "payType",column = "payType"),
            @Result(property = "payMoney",column = "payMoney"),
            @Result(property = "payStatus",column = "payStatus"),
            @Result(property = "payDate",column = "payDate"),
            @Result(property = "user",column = "payUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById"))
    })
    List<Pay> findNoPay(String username);

    @Select("select * from pays where payId=#{payId}")
    @Results({
            @Result(id=true,property = "houseId",column = "houseId"),
            @Result(property = "houseNum",column = "houseNum"),
            @Result(property = "houseBuild",column = "houseBuild"),
            @Result(property = "houseStatus",column = "houseStatus"),
            @Result(property = "houseDate",column = "houseDate"),
            @Result(property = "user",column = "houseUserId",javaType = User.class,one = @One(select = "com.hbx.residential_service.dao.UserDao.findById"))
    })
    Pay findPayById(int payId);

    @Update("update pays set payStatus=#{payStatus},payDate=#{payDate} where payId=#{payId}")
    void UpadatePay(Pay pay);

    @Insert("insert into owners(username,userpwd,usersex,userphone) values(#{username},#{userpwd},#{usersex},#{userphone})")
    void userRegister(User user);

    @Update("update owners set userpwd=#{userpwd} where userid=#{userid}")
    void UpdatePwd(User user);

    @Select("select * from owners where userid = #{userid}")
    User findUpdHouseById(int userid);

    @Update("update owners set username=#{username},usersex=#{usersex},userphone=#{userphone} where userid=#{userid}")
    void UpdateById(User user);

    @Select("select * from owners where username like #{content}")
    List<User> fsByUsername(@Param("content") String content, int page, int size);
}
